package main.app.view.screens
{
	import com.adobe.serialization.json.JSON;
	
	import feathers.controls.Button;
	import feathers.controls.Header;
	import feathers.controls.Screen;
	import feathers.events.FeathersEventType;
	
	import main.app.model.CenterModel;
	
	import starling.events.Event;
	
	public class LoginMenu extends Screen
	{
		private var _header:Header;
        private var _loginButton:Button;
		private var _registerButton:Button;
		
		override protected function initialize():void
		{
		  _header = new Header();
		  _header.title = "Login Menu";
		  addChild(_header);
		  
		  _loginButton = new Button();
		  _loginButton.label = "登陆";
		  _loginButton.width = 100;
		  _loginButton.x = Constants.STAGE_WIDTH/2;
		  _loginButton.y = Constants.STAGE_HEIGHT/2;
		  _loginButton.addEventListener(Event.TRIGGERED,__onLoginButtonPressed);
		  
		  _registerButton = new Button();
		  _registerButton.label = "注册";
		  _registerButton.width = 100;
		  _registerButton.x = Constants.STAGE_WIDTH/2;
		  _registerButton.y = Constants.STAGE_HEIGHT/2+50;
		  _registerButton.addEventListener(Event.TRIGGERED,__onRegisterButtonPressed);
		  
		  CenterModel.getInstance().loginModel.addEventListener("aa",loginSussess);
		  addChild(_loginButton);
		  addChild(_registerButton);
		  
		}
		
		private function __onRegisterButtonPressed(evt:Event):void
		{
			dispatchEventWith( ScreenEvents.SHOW_REGISTER_MENU);  
		}
		
		private function __onLoginButtonPressed(evt:Event):void
		{
			dispatchEventWith( ScreenEvents.SHOW_MAIN_MENU );  //不需要联网测试就取消注释
			
			var vo:Object = new Object();
			vo.command = 0;
			vo.userId = 1;
			vo.userName = "aa";
			trace(com.adobe.serialization.json.JSON.encode(vo));
			Root.http.reuqestServer(com.adobe.serialization.json.JSON.encode(vo));
		}
		
		private function loginSussess(e:Event):void
		{
			dispatchEventWith( ScreenEvents.SHOW_MAIN_MENU );    
			
		}
		
		override protected function draw():void
		{
			this._header.width = this.actualWidth;
			this._header.validate();
		}
		
		//清理函数，很重要
		override protected function screen_removedFromStageHandler(event:Event):void
		{
			this.removeChildren(0,-1,true);
			CenterModel.getInstance().loginModel.removeEventListeners();
		}
	}
}